﻿using SpiderStudy.Utils.Currency;
using SpiderStudy.Utils.Currency.FileUtils;
using SpiderStudy.Utils.Helper;
using SpiderStudy.Utils.Html;
using System;
using System.Collections.Generic;

namespace SpiderStudy.WebSite
{
    internal class WebSiteHelper
    {
        /// <summary>
        /// 文章内容信息 Title Content Shtml
        /// </summary>
        public struct ArticleInfo
        {
            private string Content;
            private string Title;
            private string Shtml;

            public string Content1 { get => Content; set => Content = value; }
            public string Title1 { get => Title; set => Title = value; }
            public string Shtml1 { get => Shtml; set => Shtml = value; }
        }

        /// <summary>
        /// 解析并获取所有连接
        /// </summary>
        /// <param name="ListLink"></param>
        /// <returns></returns>
        public List<string> AnalysisArticleLink(string ListLink, string Xpath)
        {
            String Shtml = WebHelper.HttpGet(ListLink);

            return HtmlHelper.GetLinkVlaueList(Shtml, Xpath);
        }

        /// <summary>
        /// 扫描并解析单个文章  返回结构体 ArticleInfo
        /// </summary>
        /// <param name="singleArticleLink"></param>
        public ArticleInfo AnalysisSingleArticle(string SingleArticleLink, string TitleXpath, string ContentXpath)
        {
            String Shtml = WebHelper.HttpGet(SingleArticleLink);
            string Title = HtmlHelper.GetFirstValue(HtmlHelper.GetTextValueList(Shtml, TitleXpath));
            string Content = HtmlHelper.GetFirstValue(HtmlHelper.GetHtmlValueList(Shtml, ContentXpath));
            ArticleInfo ArticleInfoTemp = new ArticleInfo() { Content1 = Content, Title1 = Title, Shtml1 = Shtml };
            return ArticleInfoTemp;
        }

        /// <summary>
        /// 保存数据到指定文件夹&文件
        /// </summary>
        /// <param name="FileName"></param>
        /// <param name="FloderClassify"></param>
        /// <param name="TextList"></param>
        public void SaveSpiderData(string FileName, string FloderClassify, List<string> TextList)
        {
            String SaveTitlePath = StaticValue.TempPath + TextHelper.ReplaceBadChar(FloderClassify) + "\\";
            FloderHelper.FloderExits(SaveTitlePath, true);
            FileHelper.AppendUTF8List(SaveTitlePath + FileName, TextList);
        }

        /// <summary>
        /// 数据去重复
        /// </summary>
        /// <param name="FileName"></param>
        /// <param name="FloderClassify"></param>
        public void DataToRepeat(string FileName, string FloderClassify)
        {
            String SaveFilePath = StaticValue.TempPath + TextHelper.ReplaceBadChar(FloderClassify) + "\\" + FileName;
            List<string> TempList = new List<string>();

            foreach (string Line in FileHelper.ReadAllLine(SaveFilePath))
            {
                if (!TempList.Contains(Line) && Line != "")
                    TempList.Add(Line);
            }
            FileHelper.WriteUTF8List(SaveFilePath, TempList);
        }
    }
}